<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 邮件回调类
 * @author Calvin<hongbinfu@163.com>
 *
 */
class Email_callback extends CI_Controller{
	
	public function __construct()
	{
		parent::__construct();
		$this->load->service('login_service');
		$this->load->service('user_info_service');
		$this->load->service('register_service');
		$this->load->service('user_verify_service');
		$this->load->library("form_validation");
		$this->config->load("form_validation");
	}

	/**
	 * 验证用户为验证的邮箱
	 * @param string $code
	 * @return bool
	 */
	public function valid_user_email($code="")
	{
		if(empty($code))
		{
			show_404();
		}
		$result = $this->user_verify_service->check_email_verify_code($code,"",User_verify_service::PROCESS_VALID_EMAIL,TRUE,TRUE);
		if($result===false)
		{
			$data = array(
				'title' => '验证邮箱',
				'model' => 'link_lost'
			);
			$this->template->view('user/tips.tpl',$data);
			return false;
		}
		else
		{
			$this->user_info_service->update_user_info($result['uid'],array("email"=>$result['receiver'],"n_email"=>""));
			if($this->login_service->is_login())
			{
				$this->login_service->reload_user_info();
			}
			$data=array(
				"title"=>"邮箱验证成功",
				"type" =>"success",
				"msg" =>"你的邮箱已经绑定成功",
				"url" =>"/user/user_info",
				"time"=>5
			);
			$this->template->view("common/message.tpl",$data);
		}
	}


	/**
	 * 验证用户的新邮箱
	 * @param string $code
	 * @return bool
	 */
	public function valid_new_email($code="")
	{
		if(empty($code))
		{
			show_404();
		}
		$result = $this->user_verify_service->check_email_verify_code($code,"",User_verify_service::PROCESS_MODIFY_EMAIL,TRUE,TRUE);
		if($result===false)
		{
			$data = array(
				'title' => '验证邮箱',
				'model' => 'link_lost'
			);
			$this->template->view('user/tips.tpl',$data);
			return false;
		}
		else
		{
			$this->user_info_service->update_user_info($result['uid'],array("email"=>$result['receiver']));
			if($this->login_service->is_login())
			{
				$this->login_service->reload_user_info();
			}
			$data=array(
				"title"=>"邮箱修改成功",
				"type" =>"success",
				"msg" =>"你的新邮箱已经绑定成功",
				"url" =>"/user/user_info",
				"time"=>5
			);
			$this->template->view("common/message.tpl",$data);
		}
	}



}